home *** CD-ROM | disk | FTP | other *** search
/ SVM Mac 58 / CD-ROM N°58.iso / navigateurs / Netscape Folder / chrome / sidebar / content / default / sidebarOverlay.js < prev    next >
Encoding:
JavaScript  |  2000-04-19  |  15.4 KB  |  455 lines  |  [TEXT/MOSS]

  1. /* -*- Mode: Java; tab-width: 4; insert-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Mozilla Public License
  4.  * Version 1.1 (the "License"); you may not use this file except in
  5.  * compliance with the License. You may obtain a copy of the License at
  6.  * http://www.mozilla.org/MPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS IS"
  9.  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
  10.  * the License for the specific language governing rights and
  11.  * limitations under the License.
  12.  *
  13.  * The Original Code is Mozilla Communicator client code, released
  14.  * March 31, 1998.
  15.  *
  16.  * The Initial Developer of the Original Code is Netscape
  17.  * Communications Corporation. Portions created by Netscape are
  18.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  19.  * Rights Reserved.
  20.  */
  21.  
  22. // the rdf service
  23. var rdf_uri = 'component://netscape/rdf/rdf-service'
  24. var RDF = Components.classes[rdf_uri].getService()
  25. RDF = RDF.QueryInterface(Components.interfaces.nsIRDFService)
  26.  
  27. var NC = "http://home.netscape.com/NC-rdf#";
  28.  
  29. // the magic number to find panels.rdf
  30. var PANELS_RDF_FILE = 66626;
  31. var SIDEBAR_VERSION = "0.0";
  32.  
  33. // the default sidebar:
  34. var sidebarObj = new Object;
  35. sidebarObj.never_built = true;
  36.  
  37. function debug(msg) {
  38.   // uncomment for noise
  39.   //dump(msg+"\n");
  40. }
  41.  
  42. var panel_observer = new Object;
  43.  
  44. panel_observer = {
  45.   OnAssert : function(src,prop,target) { debug ("*** assert");},
  46.   OnUnassert : function(src,prop,target) {
  47.     // Wait for unassert that marks the end of the customize changes.
  48.     // See customize.js for where this is unasserted.
  49.     if (prop == RDF.GetResource(NC + "inbatch")) {
  50.       sidebar_open_default_panel(100, 0);
  51.     }
  52.   },
  53.   OnChange : function(src,prop,old_target,new_target) {},
  54.   OnMove : function(old_src,new_src,prop,target) {}
  55. }
  56.  
  57.  
  58. function get_sidebar_datasource_uri(panels_file_id) {
  59.   try {
  60.     var locator_interface = Components.interfaces.nsIFileLocator;
  61.     var locator_prog_id = 'component://netscape/filelocator';
  62.     var locator_service = Components.classes[locator_prog_id].getService();
  63.     // use the fileLocator to look in the profile directory
  64.     // to find 'panels.rdf', which is the
  65.     // database of the user's currently selected panels.
  66.     locator_service = locator_service.QueryInterface(locator_interface);
  67.  
  68.     // if <profile>/panels.rdf doesn't exist, GetFileLocation() will copy
  69.     // bin/defaults/profile/panels.rdf to <profile>/panels.rdf
  70.     var sidebar_file = locator_service.GetFileLocation(panels_file_id);
  71.  
  72.     if (!sidebar_file.exists()) {
  73.       // this should not happen, as GetFileLocation() should copy
  74.       // defaults/panels.rdf to the users profile directory
  75.       return null;
  76.     }
  77.  
  78.     debug("sidebar uri is " + sidebar_file.URLString);
  79.     return sidebar_file.URLString;
  80.   }
  81.   catch (ex) {
  82.     // this should not happen
  83.     debug("Error: Unable to load panels file.\n");
  84.     return null;
  85.   }
  86. }
  87.  
  88. function sidebar_overlay_init() {
  89.   sidebarObj.datasource_uri = get_sidebar_datasource_uri(PANELS_RDF_FILE);
  90.   sidebarObj.resource = 'urn:sidebar:current-panel-list';
  91.   
  92.   sidebarObj.master_datasources = get_remote_datasource_url();
  93.   sidebarObj.master_datasources += " chrome://sidebar/content/local-panels.rdf";
  94.   sidebarObj.master_resource = 'urn:sidebar:master-panel-list';
  95.   sidebarObj.component = document.location.href;
  96.  
  97.   // Initialize the display
  98.   var sidebar_element  = document.getElementById('sidebar-box');
  99.   var sidebar_menuitem = document.getElementById('menu_sidebar');
  100.   if (sidebar_element.getAttribute('hidden') == 'true') {
  101.     if (sidebar_menuitem) {
  102.       sidebar_menuitem.setAttribute('checked', 'false');
  103.     }
  104.   } else {
  105.     if (sidebar_menuitem) {
  106.       sidebar_menuitem.setAttribute('checked', 'true');
  107.     }
  108.     
  109.     if (sidebarObj.never_built) {
  110.       sidebarObj.never_built = false;
  111.  
  112.       debug("sidebar = " + sidebarObj);
  113.       debug("sidebarObj.resource = " + sidebarObj.resource);
  114.       debug("sidebarObj.datasource_uri = " + sidebarObj.datasource_uri);
  115.  
  116.       // Add the user's current panel choices to the template builder,
  117.       // which will aggregate it with the other datasources that describe
  118.       // the individual panel's title, customize URL, and content URL.
  119.       var panels = document.getElementById('sidebar-panels');
  120.       panels.database.AddDataSource(RDF.GetDataSource(sidebarObj.datasource_uri));
  121.       
  122.       // The stuff on the bottom
  123.       var panels_bottom = document.getElementById('sidebar-panels-bottom');
  124.       panels_bottom.database.AddDataSource(RDF.GetDataSource(sidebarObj.datasource_uri));
  125.     
  126.       debug("Adding observer to database.");
  127.       panels.database.AddObserver(panel_observer);
  128.  
  129.       // XXX This is a hack to force re-display
  130.       panels.setAttribute('ref', sidebarObj.resource);
  131.  
  132.       // XXX This is a hack to force re-display
  133.       panels_bottom.setAttribute('ref', sidebarObj.resource);
  134.     }
  135.     sidebar_open_default_panel(100, 0);
  136.   }
  137. }
  138.  
  139. function sidebar_overlay_destruct() {
  140.     var panels = document.getElementById('sidebar-panels');
  141.     debug("Removeing observer from database.");
  142.     panels.database.RemoveObserver(panel_observer);
  143. }
  144.  
  145. // Get the template for the available panels url from preferences.
  146. // Replace variables in the url:
  147. //     %LOCALE%  -->  Application locale (e.g. en-us).
  148. //     %VERSION% --> Sidebar file format version (e.g. 0.0).
  149. function get_remote_datasource_url() {
  150.   var url = '';
  151.   var prefs = Components.classes['component://netscape/preferences'];
  152.   if (prefs) {
  153.     prefs = prefs.getService();
  154.   }
  155.   if (prefs) {
  156.     prefs = prefs.QueryInterface(Components.interfaces.nsIPref);
  157.   }
  158.   if (prefs) {
  159.     try {
  160.       url = prefs.CopyCharPref("sidebar.customize.all_panels.url");
  161.       url = url.replace(/%SIDEBAR_VERSION%/g, SIDEBAR_VERSION);
  162.  
  163.       var locale_progid = 'component://netscape/intl/nslocaleservice';
  164.       var locale = Components.classes[locale_progid].getService();
  165.       locale = locale.QueryInterface(Components.interfaces.nsILocaleService);
  166.       locale = locale.GetLocaleComponentForUserAgent();
  167.       locale = locale.toLowerCase();
  168.       url = url.replace(/%LOCALE%/g, locale);
  169.  
  170.       debug("Remote url is " + url);
  171.     } catch(ex) {
  172.       debug("Unable to get remote url pref. What now? "+ex);
  173.     }
  174.   }
  175.   return url;
  176. }
  177.  
  178. function sidebar_open_default_panel(wait, tries) {
  179.   var panels_top  = document.getElementById('sidebar-panels');
  180.   var target = panels_top.getAttribute('open-panel-src');
  181.   var top_headers = panels_top.childNodes;
  182.  
  183.   debug("sidebar_open_default_panel("+wait+","+tries+")");
  184.  
  185.   if (top_headers.length <= 1) {
  186.     if (tries < 5) {
  187.       // No children yet, try again later
  188.       setTimeout('sidebar_open_default_panel('+(wait*2)+','+(tries+1)+')',wait);
  189.     } else {
  190.       // No panels.
  191.       // XXX This should load some help page instead of about:blank.
  192.       var iframe = document.getElementById('sidebar-content');
  193.       iframe.setAttribute('src', 'about:blank');
  194.     }
  195.     return;
  196.   }
  197.  
  198.   select_panel(target);
  199. }
  200.  
  201. function select_panel(target) {
  202.   var panels_top = document.getElementById('sidebar-panels');
  203.   var iframe = document.getElementById('sidebar-content');
  204.   var top_headers = panels_top.childNodes;
  205.  
  206.   debug("select_panel("+target+")");
  207.  
  208.   iframe.setAttribute('src', 'about:blank');
  209.  
  210.   var select_index = find_panel(top_headers, target);
  211.  
  212.   if (!select_index) {
  213.     // Target not found. Pick the first panel by default.
  214.     // It is at index 1 because the template is at index 0.
  215.     debug("select_panel: target not found, choosing last panel, index "+top_headers.length+"\n");
  216.     select_index = pick_default_panel(top_headers);
  217.     target = top_headers.item(select_index).getAttribute('iframe-src');
  218.   }
  219.  
  220.   // Update the content area if necessary.
  221.   if (iframe.getAttribute('src') != target) {
  222.     iframe.setAttribute('src', target);
  223.   }
  224.   if (panels_top.getAttribute('open-panel-src') != target) {
  225.     panels_top.setAttribute('open-panel-src', target);
  226.   }
  227.  
  228.   update_headers(select_index);
  229. }
  230.  
  231. function find_panel(panels, target) {
  232.   if (target && target != '') {
  233.     // Find the index of the selected panel
  234.     for (var ii=1; ii < panels.length; ii++) {
  235.       var item = panels.item(ii);
  236.       
  237.       if (item.getAttribute('iframe-src') == target) {
  238.         if (is_excluded(item)) {
  239.           debug("find_panel: Found panel at index, "+ii+", but it is excluded");
  240.           return 0;
  241.         } else {
  242.           // Found it!
  243.           debug("find_panel: Found panel at index, "+ii);
  244.           return ii;
  245.         }
  246.       }
  247.     }
  248.   }
  249.   return 0;
  250. }
  251.  
  252. function pick_default_panel(panels) {
  253.   last_non_excluded_index = null;
  254.   for (var ii=1; ii < panels.length; ii++) {
  255.     if (!is_excluded(panels.item(ii))) {
  256.       last_non_excluded_index = ii;
  257.     }
  258.   }
  259.   return last_non_excluded_index;
  260. }
  261.  
  262. function is_excluded(item) {
  263.   var exclude = item.getAttribute('exclude');
  264.   var src = item.getAttribute('iframe-src');
  265.   debug("src="+src);
  266.   debug("exclude="+exclude);
  267.   return exclude && exclude != '' && exclude.indexOf(sidebarObj.component) != -1;
  268. }
  269.  
  270. function update_headers(index) {
  271.   var top_headers = document.getElementById('sidebar-panels').childNodes;
  272.   var bottom_headers = document.getElementById('sidebar-panels-bottom').childNodes;
  273.  
  274.   for (var ii=1; ii < top_headers.length; ii++) {
  275.     var top_item = top_headers.item(ii);
  276.     var bottom_item = bottom_headers.item(ii);
  277.  
  278.     if (is_excluded(top_item)) {
  279.       top_item.setAttribute('hidden','true');
  280.       bottom_item.setAttribute('hidden','true');
  281.     } else if (ii < index) { 
  282.       top_item.removeAttribute('selected');
  283.       top_item.removeAttribute('hidden');
  284.       bottom_item.setAttribute('hidden','true');
  285.     } else if (ii == index) {
  286.       top_item.setAttribute('selected','true');
  287.       top_item.removeAttribute('hidden');
  288.       bottom_item.setAttribute('hidden','true');
  289.     } else {
  290.       top_item.setAttribute('hidden','true');
  291.       bottom_item.removeAttribute('hidden');
  292.       bottom_item.removeAttribute('selected');
  293.     }
  294.   }
  295. }
  296.  
  297.  
  298. // Change the sidebar content to the selected panel.
  299. // Called when a panel title is clicked.
  300. function SidebarSelectPanel(titledbutton) {
  301.   var target = titledbutton.getAttribute('iframe-src');
  302.   var last_src = titledbutton.parentNode.getAttribute('open-panel-src');
  303.  
  304.   if (target == last_src) {
  305.     // XXX Maybe this should reload the content?
  306.     return;
  307.   }
  308.  
  309.   select_panel(target);
  310. }
  311.  
  312. // No one is calling this right now.
  313. function SidebarReload() {
  314.   sidebar_open_default_panel(100, 0);
  315. }
  316.  
  317. // Set up a lame hack to avoid opening two customize
  318. // windows on a double click.
  319. var gDisableCustomize = false;
  320. function enable_customize() {
  321.   gDisableCustomize = false;
  322. }
  323.  
  324. // Bring up the Sidebar customize dialog.
  325. function SidebarCustomize() {
  326.   // Use a single sidebar customize dialog
  327.   var cwindowManager = Components.classes['component://netscape/rdf/datasource?name=window-mediator'].getService();
  328.   var iwindowManager = Components.interfaces.nsIWindowMediator;
  329.   var windowManager  = cwindowManager.QueryInterface(iwindowManager);
  330.  
  331.   var customizeWindow = windowManager.getMostRecentWindow('sidebar:customize');
  332.  
  333.   if (customizeWindow) {
  334.     debug("Reuse existing customize dialog");
  335.     customizeWindow.focus();
  336.   } else {
  337.     debug("Open a new customize dialog");
  338.  
  339.     if (false == gDisableCustomize) {
  340.       gDisableCustomize = true;
  341.  
  342.       var panels = document.getElementById('sidebar-panels');
  343.       
  344.       customizeWindow = window.openDialog(
  345.                           'chrome://sidebar/content/customize.xul',
  346.                           '_blank','chrome,resizable',
  347.                           sidebarObj.master_datasources,
  348.                           sidebarObj.master_resource,
  349.                           sidebarObj.datasource_uri,
  350.                           sidebarObj.resource);
  351.       setTimeout(enable_customize, 2000);
  352.     }
  353.   }
  354. }
  355.  
  356. // Show/Hide the entire sidebar.
  357. // Envoked by the "View / Sidebar" menu option.
  358. function SidebarShowHide() {
  359.   var sidebar_box = document.getElementById('sidebar-box');
  360.   var sidebar_splitter = document.getElementById('sidebar-splitter');
  361.   var is_hidden = sidebar_box.getAttribute('hidden');
  362.  
  363.   if (is_hidden && is_hidden == "true") {
  364.     debug("Showing the sidebar");
  365.     sidebar_box.removeAttribute('hidden');
  366.     sidebar_splitter.removeAttribute('hidden');
  367.     sidebar_overlay_init();
  368.   } else {
  369.     debug("Hiding the sidebar");
  370.     sidebar_box.setAttribute('hidden','true');
  371.     sidebar_splitter.setAttribute('hidden','true');
  372.   }
  373.   // Immediately save persistent values
  374.   document.persist('sidebar-box', 'hidden');
  375.   persist_width();
  376. }
  377.  
  378. // SidebarExpandCollapse() - Respond to grippy click.
  379. function SidebarExpandCollapse() {
  380.   // XXX Mini hack. Persist isn't working too well. Force the persist,
  381.   // but wait until the change has commited.
  382.   setTimeout("document.persist('sidebar-box', 'collapsed');",100);
  383. }
  384.  
  385. function PersistHeight() {
  386.   // XXX Mini hack. Persist isn't working too well. Force the persist,
  387.   // but wait until the last drag has been committed.
  388.   // May want to do something smarter here like only force it if the 
  389.   // width has really changed.
  390.   setTimeout("document.persist('sidebar-panels-box','height');",100);
  391. }
  392.  
  393. function persist_width() {
  394.   // XXX Partial workaround for bug #19488.
  395.   var sidebar_box = document.getElementById('sidebar-box');
  396.  
  397.   var width = sidebar_box.getAttribute('width');
  398.   if (width && (width > 410 || width < 15)) {
  399.     sidebar_box.setAttribute('width',168);
  400.   }
  401.  
  402.   // XXX Mini hack. Persist isn't working too well. Force the persist,
  403.   // but wait until the width change has commited.
  404.   setTimeout("document.persist('sidebar-box', 'width');",100);
  405. }
  406.  
  407. function SidebarFinishDrag() {
  408.  
  409.   // XXX Semi-hack for bug #16516.
  410.   // If we had the proper drag event listener, we would not need this
  411.   // timeout. The timeout makes sure the width is written to disk after
  412.   // the sidebar-box gets the newly dragged width.
  413.   setTimeout("persist_width()",100);
  414. }
  415.  
  416. /*==================================================
  417. // Handy debug routines
  418. //==================================================
  419. function dump_attributes(node,depth) {
  420.   var attributes = node.attributes;
  421.   var indent = "| | | | | | | | | | | | | | | | | | | | | | | | | | | | | . ";
  422.  
  423.   if (!attributes || attributes.length == 0) {
  424.     debug(indent.substr(indent.length - depth*2) + "no attributes");
  425.   }
  426.   for (var ii=0; ii < attributes.length; ii++) {
  427.     var attr = attributes.item(ii);
  428.     debug(indent.substr(indent.length - depth*2) + attr.name +"="+attr.value);
  429.   }
  430. }
  431.  
  432. function dump_tree(node, depth) {
  433.   if (!node) {
  434.     debug("dump_tree: node is null");
  435.   }
  436.   var indent = "| | | | | | | | | | | | | | | | | | | | | | | | | | | | | + ";
  437.   debug(indent.substr(indent.length - depth*2) + node.nodeName);
  438.   if (node.nodeName != "#text") {
  439.     //debug(" id="+node.getAttribute('id'));
  440.     dump_attributes(node, depth);
  441.   }
  442.   var kids = node.childNodes;
  443.   for (var ii=0; ii < kids.length; ii++) {
  444.     dump_tree(kids[ii], depth + 1);
  445.   }
  446. }
  447. //==================================================
  448. // end of handy debug routines
  449. //==================================================*/
  450.  
  451.  
  452. // Install our load handler
  453. addEventListener("load", sidebar_overlay_init, false);
  454. addEventListener("unload", sidebar_overlay_destruct, false);
  455.